/*     */ package com.zimbra.cs.db;
/*     */ 
/*     */ import com.google.common.base.Joiner;
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.redolog.Version;
/*     */ import java.io.BufferedWriter;
/*     */ import java.io.File;
/*     */ import java.io.FileWriter;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintStream;
/*     */ import java.io.Writer;
/*     */ import java.sql.PreparedStatement;
/*     */ import java.sql.ResultSet;
/*     */ import java.sql.SQLException;
/*     */ import java.sql.Statement;
/*     */ import java.util.HashMap;
/*     */ import java.util.Map;
/*     */ import java.util.Properties;
/*     */ import org.apache.commons.cli.CommandLine;
/*     */ import org.apache.commons.cli.Options;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class MySQL
/*     */   extends Db
/*     */ {
/*     */   private Map<Db.Error, Integer> mErrorCodes;
/*     */   
/*     */   MySQL()
/*     */   {
/*  46 */     this.mErrorCodes = new HashMap(6);
/*     */     
/*  48 */     this.mErrorCodes.put(Db.Error.DEADLOCK_DETECTED, Integer.valueOf(1213));
/*  49 */     this.mErrorCodes.put(Db.Error.DUPLICATE_ROW, Integer.valueOf(1062));
/*  50 */     this.mErrorCodes.put(Db.Error.FOREIGN_KEY_NO_PARENT, Integer.valueOf(1216));
/*  51 */     this.mErrorCodes.put(Db.Error.FOREIGN_KEY_CHILD_EXISTS, Integer.valueOf(1451));
/*  52 */     this.mErrorCodes.put(Db.Error.NO_SUCH_DATABASE, Integer.valueOf(1146));
/*  53 */     this.mErrorCodes.put(Db.Error.NO_SUCH_TABLE, Integer.valueOf(1146));
/*  54 */     this.mErrorCodes.put(Db.Error.TABLE_FULL, Integer.valueOf(1114));
/*     */   }
/*     */   
/*     */   boolean supportsCapability(Db.Capability capability)
/*     */   {
/*  59 */     switch (capability) {
/*  60 */     case AVOID_OR_IN_WHERE_CLAUSE:  return false;
/*  61 */     case BITWISE_OPERATIONS:  return true;
/*  62 */     case BOOLEAN_DATATYPE:  return true;
/*  63 */     case CASE_SENSITIVE_COMPARISON:  return false;
/*  64 */     case CAST_AS_BIGINT:  return false;
/*  65 */     case CLOB_COMPARISON:  return true;
/*  66 */     case DISABLE_CONSTRAINT_CHECK:  return true;
/*  67 */     case FILE_PER_DATABASE:  return false;
/*  68 */     case LIMIT_CLAUSE:  return true;
/*  69 */     case MULTITABLE_UPDATE:  return true;
/*  70 */     case NON_BMP_CHARACTERS:  return false;
/*  71 */     case ON_DUPLICATE_KEY:  return true;
/*  72 */     case ON_UPDATE_CASCADE:  return true;
/*  73 */     case READ_COMMITTED_ISOLATION:  return true;
/*  74 */     case REPLACE_INTO:  return true;
/*  75 */     case REQUEST_UTF8_UNICODE_COLLATION:  return true;
/*  76 */     case ROW_LEVEL_LOCKING:  return true;
/*  77 */     case UNIQUE_NAME_INDEX:  return true;
/*  78 */     case SQL_PARAM_LIMIT:  return false;
/*  79 */     case DUMPSTER_TABLES:  return true;
/*     */     }
/*  81 */     return false;
/*     */   }
/*     */   
/*     */   boolean compareError(SQLException e, Db.Error error)
/*     */   {
/*  86 */     Integer code = (Integer)this.mErrorCodes.get(error);
/*  87 */     return (code != null) && (e.getErrorCode() == code.intValue());
/*     */   }
/*     */   
/*     */   String forceIndexClause(String index)
/*     */   {
/*  92 */     return " FORCE INDEX (" + index + ')';
/*     */   }
/*     */   
/*     */   String getIFNULLClause(String expr1, String expr2)
/*     */   {
/*  97 */     return "IFNULL(" + expr1 + ", " + expr2 + ")";
/*     */   }
/*     */   
/*     */   public String bitAND(String expr1, String expr2)
/*     */   {
/* 102 */     return expr1 + " & " + expr2;
/*     */   }
/*     */   
/*     */   public String bitANDNOT(String expr1, String expr2)
/*     */   {
/* 107 */     return expr1 + " & ~" + expr2;
/*     */   }
/*     */   
/*     */   DbPool.PoolConfig getPoolConfig()
/*     */   {
/* 112 */     return new MySQLConfig();
/*     */   }
/*     */   
/*     */   public boolean databaseExists(DbPool.DbConnection conn, String dbname) throws ServiceException
/*     */   {
/* 117 */     PreparedStatement stmt = null;
/* 118 */     ResultSet rs = null;
/* 119 */     int numSchemas = 0;
/*     */     try
/*     */     {
/* 122 */       stmt = conn.prepareStatement("SELECT COUNT(*) FROM INFORMATION_SCHEMA.SCHEMATA WHERE schema_name = ?");
/*     */       
/*     */ 
/* 125 */       stmt.setString(1, dbname);
/* 126 */       rs = stmt.executeQuery();
/* 127 */       rs.next();
/* 128 */       numSchemas = rs.getInt(1);
/*     */     } catch (SQLException e) {
/* 130 */       throw ServiceException.FAILURE("Unable to determine whether database exists", e);
/*     */     } finally {
/* 132 */       DbPool.closeResults(rs);
/* 133 */       DbPool.closeStatement(stmt);
/*     */     }
/*     */     
/* 136 */     return numSchemas > 0;
/*     */   }
/*     */   
/*     */   public void enableStreaming(Statement stmt)
/*     */     throws SQLException
/*     */   {
/* 142 */     if (LC.jdbc_results_streaming_enabled.booleanValue()) {
/* 143 */       stmt.setFetchSize(Integer.MIN_VALUE);
/*     */     }
/*     */   }
/*     */   
/*     */   protected class MySQLConfig extends DbPool.PoolConfig {
/*     */     MySQLConfig() {
/* 149 */       this.mDriverClassName = getDriverClassName();
/* 150 */       this.mPoolSize = 100;
/* 151 */       this.mRootUrl = getRootUrl();
/* 152 */       this.mConnectionUrl = (this.mRootUrl + "zimbra");
/* 153 */       this.mLoggerUrl = null;
/* 154 */       this.mSupportsStatsCallback = true;
/* 155 */       this.mDatabaseProperties = getDBProperties();
/*     */       
/*     */ 
/* 158 */       String maxActive = (String)this.mDatabaseProperties.get("maxActive");
/* 159 */       if (maxActive != null) {
/*     */         try {
/* 161 */           this.mPoolSize = Integer.parseInt(maxActive);
/*     */         } catch (NumberFormatException nfe) {
/* 163 */           ZimbraLog.system.warn("exception parsing 'maxActive' pref; defaulting pool size to " + this.mPoolSize, nfe);
/*     */         }
/*     */       }
/* 166 */       ZimbraLog.misc.debug("Setting connection pool size to " + this.mPoolSize);
/*     */     }
/*     */     
/*     */     protected String getDriverClassName() {
/* 170 */       return "com.mysql.jdbc.Driver";
/*     */     }
/*     */     
/*     */     protected String getRootUrl() {
/* 174 */       return "jdbc:mysql://address=(protocol=tcp)(host=" + LC.mysql_bind_address.value() + ")(port=" + LC.mysql_port.value() + ")/";
/*     */     }
/*     */     
/*     */     protected Properties getDBProperties() {
/* 178 */       Properties props = new Properties();
/*     */       
/* 180 */       props.put("cacheResultSetMetadata", "true");
/* 181 */       props.put("cachePrepStmts", "true");
/*     */       
/* 183 */       props.put("prepStmtCacheSize", "25");
/*     */       
/* 185 */       props.put("autoReconnect", "true");
/* 186 */       props.put("useUnicode", "true");
/* 187 */       props.put("characterEncoding", "UTF-8");
/* 188 */       props.put("dumpQueriesOnException", "true");
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 196 */       String prefix = "zimbra_mysql_connector_";
/* 197 */       for (String key : LC.getAllKeys()) {
/* 198 */         if (key.startsWith("zimbra_mysql_connector_"))
/*     */         {
/* 200 */           String prop = key.substring("zimbra_mysql_connector_".length());
/* 201 */           if ((prop.length() > 0) && (!prop.equalsIgnoreCase("logger"))) {
/* 202 */             props.put(prop, LC.get(key));
/* 203 */             ZimbraLog.system.info("Setting mysql connector property: " + prop + "=" + LC.get(key));
/*     */           }
/*     */         }
/*     */       }
/*     */       
/* 208 */       props.put("user", LC.zimbra_mysql_user.value());
/* 209 */       props.put("password", LC.zimbra_mysql_password.value());
/*     */       
/* 211 */       return props;
/*     */     }
/*     */   }
/*     */   
/*     */   public String toString()
/*     */   {
/* 217 */     return "MySQL";
/*     */   }
/*     */   
/* 220 */   private final String sTableName = "zimbra.flush_enforcer";
/* 221 */   private final String sCreateTable = "CREATE TABLE IF NOT EXISTS zimbra.flush_enforcer (dummy_column INTEGER) ENGINE = InnoDB";
/*     */   
/* 223 */   private final String sDropTable = "DROP TABLE IF EXISTS zimbra.flush_enforcer";
/*     */   
/*     */ 
/*     */ 
/*     */   public synchronized void flushToDisk()
/*     */   {
/* 229 */     DbPool.DbConnection conn = null;
/* 230 */     PreparedStatement createStmt = null;
/* 231 */     PreparedStatement dropStmt = null;
/* 232 */     boolean success = false;
/*     */     try {
/*     */       try {
/* 235 */         conn = DbPool.getMaintenanceConnection();
/* 236 */         createStmt = conn.prepareStatement("CREATE TABLE IF NOT EXISTS zimbra.flush_enforcer (dummy_column INTEGER) ENGINE = InnoDB");
/* 237 */         dropStmt = conn.prepareStatement("DROP TABLE IF EXISTS zimbra.flush_enforcer");
/* 238 */         createStmt.executeUpdate();
/* 239 */         dropStmt.executeUpdate();
/* 240 */         success = true;
/*     */       } finally {
/* 242 */         DbPool.quietCloseStatement(createStmt);
/* 243 */         DbPool.quietCloseStatement(dropStmt);
/* 244 */         if (conn != null)
/* 245 */           conn.commit();
/* 246 */         DbPool.quietClose(conn);
/*     */       }
/*     */       return;
/*     */     } catch (SQLException e) {
/* 250 */       ZimbraLog.dbconn.warn("ignoring error while forcing mysql to flush innodb log to disk", e);
/*     */     }
/*     */     catch (ServiceException e) {
/* 253 */       ZimbraLog.dbconn.warn("ignoring error while forcing mysql to flush innodb log to disk", e);
/*     */     } finally {
/* 255 */       if (!success)
/*     */       {
/*     */ 
/*     */         try
/*     */         {
/*     */ 
/* 261 */           Thread.sleep(3000L);
/*     */         }
/*     */         catch (InterruptedException e1) {}
/*     */       }
/*     */     }
/*     */   }
/*     */   
/*     */   public static void main(String[] args)
/*     */   {
/* 270 */     Options options = new Options();
/* 271 */     CommandLine cl = Versions.parseCmdlineArgs(args, options);
/*     */     
/* 273 */     String outputDir = cl.getOptionValue("o");
/* 274 */     File outFile = new File(outputDir, "versions-init.sql");
/* 275 */     outFile.delete();
/*     */     try
/*     */     {
/* 278 */       String redoVer = Version.latest().toString();
/* 279 */       String outStr = "-- AUTO-GENERATED .SQL FILE - Generated by the MySQL versions tool\nUSE zimbra;\nINSERT INTO zimbra.config(name, value, description) VALUES\n\t('db.version', '103', 'db schema version'),\n\t('index.version', '2', 'index version'),\n\t('redolog.version', '" + redoVer + "', 'redolog version')\n" + ";\nCOMMIT;\n";
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 287 */       Writer output = new BufferedWriter(new FileWriter(outFile));
/* 288 */       output.write(outStr);
/* 289 */       if (output != null)
/* 290 */         output.close();
/*     */     } catch (IOException e) {
/* 292 */       System.out.println("ERROR - caught exception at\n");
/* 293 */       e.printStackTrace();
/* 294 */       System.exit(-1);
/*     */     }
/*     */   }
/*     */   
/*     */   public String concat(String... fieldsToConcat)
/*     */   {
/* 300 */     Joiner joiner = Joiner.on(", ").skipNulls();
/* 301 */     return "CONCAT(" + joiner.join(fieldsToConcat) + ")";
/*     */   }
/*     */   
/*     */   public String sign(String field)
/*     */   {
/* 306 */     return "SIGN(" + field + ")";
/*     */   }
/*     */   
/*     */   public String lpad(String field, int padSize, String padString)
/*     */   {
/* 311 */     return "LPAD(" + field + ", " + padSize + ", '" + padString + "')";
/*     */   }
/*     */   
/*     */   public String limit(int offset, int limit)
/*     */   {
/* 316 */     return "LIMIT " + offset + "," + limit;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/db/MySQL.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */